package com.huluobo.a2307monthdemo2.shop

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2307monthdemo2.R
import com.huluobo.a2307monthdemo2.adapter.CartAdapter
import com.huluobo.a2307monthdemo2.databinding.ActivityCartBinding
import com.huluobo.lib_base.bean.CartBean
import com.huluobo.lib_base.mvvm.BaseActivity
import com.huluobo.lib_base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import org.w3c.dom.Text

@AndroidEntryPoint
class CartActivity : BaseActivity<ActivityCartBinding, ShopViewModel>() {
    private lateinit var cartAdapter: CartAdapter
    private lateinit var cartBean: CartBean
    override fun initData() {
        vm.getCart()
        vm.getCartSuccess.observe(this) {
            cartAdapter.data.clear()
            cartAdapter.data.addAll(it)
            cartAdapter.notifyDataSetChanged()
        }

        vm.deleteCartSuccess.observe(this) {
            ToastUtils.showLong("删除成功")
            cartAdapter.remove(cartBean)
        }
    }

    override fun initView() {
        binding.cartRv.layoutManager = LinearLayoutManager(this)
        cartAdapter = CartAdapter()
        binding.cartRv.adapter = cartAdapter

        totalPrice()

        cartAdapter.addChildClickViewIds(
            R.id.item_cart_cb,
            R.id.item_cart_add_btn,
            R.id.item_cart_remove_btn,
            R.id.item_cart_delete_iv
        )
        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_cart_cb -> {
                    cartAdapter.data[position].isChecked = !cartAdapter.data[position].isChecked
                    (adapter.getViewByPosition(position, R.id.item_cart_cb) as CheckBox).isChecked =
                        cartAdapter.data[position].isChecked
                }

                R.id.item_cart_add_btn -> {
                    cartAdapter.data[position].count++
                    (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                        cartAdapter.data[position].count.toString()
                    val map = mapOf(
                        Const.PARAM_GOODS_ID to cartAdapter.data[position].goods_id,
                        Const.PARAM_COUNT to 1
                    )
                    vm.addToCart(map)
                }

                R.id.item_cart_remove_btn -> {
                    if (cartAdapter.data[position].count > 1) {
                        cartAdapter.data[position].count--
                        (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                            cartAdapter.data[position].count.toString()
                        val map = mapOf(
                            Const.PARAM_GOODS_ID to cartAdapter.data[position].goods_id,
                            Const.PARAM_COUNT to -1
                        )
                        vm.addToCart(map)
                    } else {
                        //调用删除方法
                        cartBean = cartAdapter.data[position]
                        deleteCart(cartBean)
                    }
                }

                R.id.item_cart_delete_iv -> {
                    cartBean = cartAdapter.data[position]
                    deleteCart(cartBean)
                }
            }
            checkAll()
            totalPrice()
        }

        binding.cartSelectAllCb.setOnCheckedChangeListener { buttonView, isChecked ->

            binding.cartSelectAllCb.setOnClickListener {
                cartAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                cartAdapter.notifyDataSetChanged()
                totalPrice()
            }
        }

        binding.cartPayNow.setOnClickListener {
            val orderList = mutableListOf<CartBean>()
            cartAdapter.data.forEach {
                if (it.isChecked) {
                    orderList.add(it)
                }
            }
            if (orderList.size > 0) {
                val intent = Intent(this, OrderActivity::class.java)
                val orderJson = Gson().toJson(orderList)
                intent.putExtra(Const.KEY_ORDER, orderJson)
                startActivity(intent)
            } else {
                ToastUtils.showLong("需要至少选中一个来支付")
            }


        }
    }

    private fun deleteCart(cartBean: CartBean) {
        val map = mapOf(
            Const.PARAM_IDS to cartBean.id
        )
        vm.deleteCart(map)
    }

    private fun totalPrice() {
        var totalPrice = 0

        cartAdapter.data.forEach {
            if (it.isChecked) {
                totalPrice += it.goods_default_price * it.count
            }
        }

        binding.cartTotalPriceTv.text = "总价:$totalPrice"
    }

    private fun checkAll() {
        val checkList = mutableListOf<Boolean>()
        cartAdapter.data.forEach {
            checkList.add(it.isChecked)
        }

        //如果检查购物车中所有的cb中没有false,就为true,如果有一个false 就为false,然后来给全选赋值
        binding.cartSelectAllCb.isChecked = false !in checkList
    }
}